package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.mq.MessageProducer;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class OrderLossMQPushTask extends OmsSimpleElasticJob {

    @Autowired
    private RoOrderService returnOrderService;

    @Autowired
    @Qualifier("kafka.message.producer")
    private MessageProducer producer;

    @Value("${kafka.topic.of.return}")
    private String topic;

    @Override
    protected Integer getFetchSize() {
        return 100;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.Bill_RO_BY_MQ;
    }

    @Override
    protected List<Integer> dealTaskStatus() {
        return Arrays.asList(TaskStatus.TASK_UN_DEAL.getCode(), TaskStatus.TASK_DEAL_FAIL.getCode());
    }

    @Override
    protected int process(OmsTask task) {
        log.info("order loss kafka push task: {} in process", new Object[]{JSON.toJSONString(task)});
        try {
            Map<String, Object> message = returnOrderService.findReturnOrderById(Long.valueOf(task.getRefCode()));
            producer.send(topic, JSON.toJSONString(message));
            return 1;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return -1;
        }
    }
}
